package caat.application.managedbean;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;



import org.icefaces.ace.component.submenu.Submenu;
import org.icefaces.ace.model.DefaultMenuModel;

import com.icesoft.faces.component.menubar.MenuItem;

import caat.application.dao.FunctionDAO;
import caat.application.entityobject.FunctionEO;
import caat.framework.AppAction;
import caat.framework.AppConstants;
import caat.framework.datamanager.Parameter;
import caat.framework.exception.DAOException;
import caat.framework.exception.DatabaseConnectionFailureException;
import caat.framework.exception.SelectStatementException;

public class FunctionMB extends AppAction {
	// bien khoi tao menu	
	private DefaultMenuModel model;
	private Connection conn = null;
	// bien khoi tao menu bar
	@SuppressWarnings("rawtypes")
	private List lstMenuBar;
	@SuppressWarnings("rawtypes")
	public List getLstMenuBar() {
		return lstMenuBar;
	}
	
	public DefaultMenuModel getModel() {
		return model;
	}

	public void setModel(DefaultMenuModel model) {
		this.model = model;
	}

	public FunctionMB() {
		super();
		
		getMenuBar();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getMenu() {
		String whereClause=null;
		Vector params = null;
		FunctionDAO funtionDAO = null;
		try {
			conn = getConnection();
			params = new Vector();
			funtionDAO = new FunctionDAO(conn);
			whereClause = "a.functionType=?";
			params.add(new Parameter(AppConstants.TYPE_MENU_ADMIN,true));
			List<FunctionEO> lstFuntion = (List<FunctionEO>) funtionDAO.getFunctionAdmin(whereClause, params);
			if(lstFuntion!=null){
				model = new DefaultMenuModel();
				for(FunctionEO menu : lstFuntion){
					/*
					 * root menu
					 * parentId = null
					 * */ 
					if(String.valueOf(menu.getParentId())!=null && menu.getParentId()==0){
						Submenu sub = setSubmenu(menu);
						model.addSubmenu(sub);
						for(int i = 0 ; i < lstFuntion.size();i++){
							FunctionEO childMenu = lstFuntion.get(i);
							if(String.valueOf(childMenu.getParentId()).equals(String.valueOf(menu.getFunctionId()))){
								sub.getChildren().add(setMenuItem(childMenu));
							}							
						}
					}
					
				}
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DatabaseConnectionFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SelectStatementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getMenuBar(){
		String whereClause=null;
		Vector params = null;
		FunctionDAO funtionDAO = null;
		try {
			conn = getConnection();
			params = new Vector();
			lstMenuBar= new ArrayList();
			funtionDAO = new FunctionDAO(conn);
			whereClause = "a.functionType=?";
			params.add(new Parameter(AppConstants.TYPE_MENU_ADMIN,true));
			List<FunctionEO> lstFuntion = (List<FunctionEO>) funtionDAO.getFunctionAdmin(whereClause, params);
			if(lstFuntion!=null){
				for(FunctionEO eo : lstFuntion){
					if(eo.getParentId()==0){
						MenuItem rootMenu = setMenuItem(eo);
						lstMenuBar.add(rootMenu);
						for(int i = 0 ; i < lstMenuBar.size();i++){
							FunctionEO childMenu = lstFuntion.get(i);
							MenuItem ChildItem = setMenuItem(childMenu);
							if(eo.getFunctionId() == childMenu.getParentId()){
								rootMenu.getChildren().add(ChildItem);
							}
							
						}
					}
				}
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DatabaseConnectionFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SelectStatementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	
	public Submenu setSubmenu(FunctionEO fEO){
		Submenu submenu = new Submenu();
		submenu.setId(fEO.getFunctionName());
		submenu.setLabel(fEO.getFunctionDes());
		return submenu;
	}
	public MenuItem setMenuItem(FunctionEO fEO){
		MenuItem menuItem = new MenuItem();
		menuItem.setId(fEO.getFunctionName());
		menuItem.setValue(fEO.getFunctionDes());		
		return menuItem;
	}
}
